<?php

namespace App\Http\Controllers;

use App\Common\Exception\ErrorException;
use App\Http\Controllers\Controller;
use App\Models\System\Rolepower;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AppController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }


    /**
     * 主页
     */
    public function index()
    {
        $isGuest = Auth::guest();
        if ($isGuest){
            return redirect()->route('login');
        }else{
            if (auth()->user()->active_flag == '0'){
                return view('apps.disable',['error'=>'该用户被禁用,请联系管理员']);
            }else{
                $user = auth()->user();
                $roles = $user->roles()->get();
                $roleids = [];
                foreach ($roles as $role) {
                    $roleids[] = $role->id;
                }
                //$intersect = array_intersect($roleids,[SYSTEM_ROLE,SCHOOLADMIN_ROLEID]);
                // if (!empty($intersect)){
                //    //管理人员
                //     return view('apps.admin');
                // }else{
                //     return view('apps.index');
                // }
                $role_id = auth()->user()->cur_role_id;
                $menus = Rolepower::where('role_id',$role_id)->pluck('actionid')->toArray();
                session(['menuroles' => [$role_id => $menus]]);
                return view('apps.index');
            }

        }
    }

    public function logout()
    {
        $url = request('url', route('home'));
        Auth::logout();
        return redirect($url);
    }
}
